/**
 * 剑指 Offer 15. 二进制中1的个数
 * https://leetcode-cn.com/problems/er-jin-zhi-zhong-1de-ge-shu-lcof/
 */

/**
 * @param {number} n - a positive integer
 * @return {number}
 */
function hammingWeight(n) {
  let count = 0;
  const max = 0b10000000000000000000000000000000;
  for (let i = 0; i < 32; i += 1) {
    if (n === max) {
      count += 1;
      break;
    }
    if (n > max) {
      count += 1;
    }
    n = (n << 1) >>> 0;
  }
  return count;
}

console.log(hammingWeight(11) === 3);
console.log(hammingWeight(128) === 1);
console.log(hammingWeight(4294967293) === 31);
